from abc import ABC

import akshare as ak
from gm.api import *

from getdata.JuejinConfig import juejin_set_token


# from L0Utils.PandasDataLocal import PandasDataLocal
#
# pro = ts.pro_api('591d1fee4ba2ebe860c911a5e58bd4d344d9458c6a9e16b09b5afd46')
#
# #查询当前所有正常上市交易的股票列表
# data = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
# pdl =PandasDataLocal()
# if not pdl.isExit('stock_list'):
#     pdl.save_hdf5(data,'stock_list')

# 构建一个获取股票列表的interface
class StockListInterface(ABC):
    def get(self):
        pass

class StockListUse:
    def __init__(self, stock_list: StockListInterface):
        self.stock_list = stock_list

    def get_all_stocks(self):
        return self.stock_list.get()
# 用akshare实现StockListInterface
class StockList_by_Akshare(StockListInterface):
    def get(self):
        stock_zh_a_spot_em_df = ak.stock_zh_a_spot_em()
        return stock_zh_a_spot_em_df


class StockList_by_juejin(StockListInterface):
    def __init__(self):
        self.all_stocks = None

    def get(self):
        juejin_set_token()
        all_stocks = get_instruments(exchanges='SZSE,SHSE', sec_types=1,
                                          fields='symbol', df = True)['symbol'].tolist()
        for item in all_stocks:
            code=item.split('.')[1]
            # print(item,code[:3])
            if code[:3] not in ['600','601','603','000','001','002','003','300']:
                all_stocks.remove(item)

                # 4118
            #判断是否开头为st
            # print(get_symbol_infos(1010,symbols=item)[0]['sec_name'])
            # if get_symbol_infos(1010,symbols=item)[0]['sec_name'].startswith('ST'):
            #     all_stocks.remove(item)
        return all_stocks
